import fetchJsonp from 'fetch-jsonp'
class Location {
	#key = '4MDBZ-YDBCI-BHSGJ-UTE5C-AHZGO-OOBBK' //实际使用时请换成自己的
	#translateRoot = `https://apis.map.qq.com/ws/coord/v1/translate?key=${this.#key}&type=1`
	#geocoderRoot = `https://apis.map.qq.com/ws/geocoder/v1/?key=${this.#key}`
	constructor(arg) {

	}
	/**
	 * 获取定位
	 */
	get() {
		return new Promise((resolve, reject) => {
			uni.getLocation({
				success: result => {
					const latitude = result.latitude //纬度
					const longitude = result.longitude //经度
					// #ifdef H5
					this.#requestH5({
						latitude: latitude,
						longitude: longitude
					}).then(res => {
						resolve({
							latitude: latitude,
							longitude: longitude,
							address: res
						})
					})
					// #endif

					// #ifndef H5
					this.#requestApp({
						latitude: latitude,
						longitude: longitude
					}).then(res => {
						resolve({
							latitude: latitude,
							longitude: longitude,
							address: res
						})
					})
					// #endif
				},
				fail: (result) => {
					console.error('获取定位失败', result)
					// #ifdef APP
					uni.showModal({
						title: "请检查定位权限",
						content: "用于图片添加水印，帮助识别操作位置",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								uni.openAppAuthorizeSetting()
							}
						}
					});
					// #endif
					reject(result)
				},
			})
		})
	}
	/**
	 * h5 位置解析请求，处理跨域问题
	 * @param {Object} param
	 */
	#requestH5(param) {
		const {
			latitude,
			longitude
		} = param
		return new Promise((resolve, reject) => {
			fetchJsonp(`${this.#translateRoot}&output=jsonp&locations=${latitude},${longitude}`, {
				jsonpCallback: 'cb',
			}).then((res) => {
				return res.json()
			}).then((res) => {
				if (res.status != 0) {
					reject(res)
					return
				}
				const location = res.locations[0]
				fetchJsonp(
					`${this.#geocoderRoot}&output=jsonp&location=${location.lat},${location.lng}`, {
						jsonpCallback: 'cb',
					}).then(res => {
					return res.json()
				}).then(res => {
					if (res.status != 0) {
						reject(res)
						return
					}
					resolve(
						`${res.result.address}-${res.result.formatted_addresses.recommend}`
					)
				})
			})
		})
	}
	/**
	 * app地址位置解析请求
	 * @param {Object} param
	 */
	#requestApp(param) {
		const {
			latitude,
			longitude
		} = param
		return new Promise((resolve, reject) => {
			uni.request({
				url: `${this.#translateRoot}&locations=${latitude},${longitude}`,
				success: (res) => {
					if (res.statusCode !== 200) {
						reject(res)
						return
					}
					const data = res.data
					if (data.status != 0) {
						reject(data)
						return
					}
					const location = data.locations[0]
					uni.request({
						url: `${this.#geocoderRoot}&location=${location.lat},${location.lng}`,
						success: (res) => {
							if (res.statusCode !== 200) {
								reject(res)
								return
							}
							const data = res.data
							if (data.status != 0) {
								reject(data)
								return
							}
							resolve(
								`${data.result.address}-${data.result.formatted_addresses.recommend}`
							)
						}
					})

				}
			})
		})
	}
}
export default Location